#include "player.h"
#include "core/scene.h"
#include "affiliate/sprite_anim.h"
#include "affiliate//collider.h"
#include "affiliate/text_label.h"
#include "raw/stats.h"
#include "raw/timer.h"

void Player::init() {
    Actor::init(); // 初始化父类
    flash_timer_ = Timer::addTimerChild(this, 0.6f);
    flash_timer_->start();
    max_speed_ = 500.0f; // 设置最大速度
    sprite_idle_ = SpriteAnim::addSpriteAnimChild(this, "resources/sprite/ghost-idle.png", 2.0f); // 创建精灵动画
    sprite_move_ = SpriteAnim::addSpriteAnimChild(this, "resources/sprite/ghost-move.png", 2.0f); // 添加精灵到子对象
    sprite_move_->setActive(false);
    collider_ = Collider::addColliderChild(this, sprite_idle_->getSize() / 2.0f);
    stats_ = Stats::addStatsChild(this);
    effect_ = Effect::addEffectChild(game_.getCurrentScene(), "resources/effect/1764.png", glm::vec2(0), 2.0f);
    effect_->setActive(false);
    weapon_thunder_ = WeaponThunder::addWeaponThunderChild(this, 2.0f, 40.0f);
}

void Player::update(float dt) {
    Actor::update(dt); // 更新父类
    velocity_ *= 0.9f; // 摩擦力
    checkState(); // 检查状态

    keyboardEvent(); // 键盘事件
    move(dt); // 移动
    syncCamera(); // 同步摄像机
    checkIsDead();
    // isAlive();
}

void Player::render() {
    if (stats_->isInvicible() && flash_timer_->getProgress() < 0.5f) {
        return;
    }
    Actor::render(); // 渲染父类
}

void Player::clean() {
    Actor::clean(); // 清理父类
    game_.getCurrentScene()->removeChild(this); // 从场景中移除
    delete this; // 删除对象
}

void Player::takeDamage(float damage) {
    if (!stats_ || stats_->isInvicible()) {
        return;
    }
    Actor::takeDamage(damage);
    game_.playSound("resources/sound/hit-flesh-02-266309.mp3");
}

bool Player::handleEvents(SDL_Event &event) {
    Actor::handleEvents(event); // 处理事件
    return false;
}

void Player::keyboardEvent() {
    auto currentKeyState = SDL_GetKeyboardState(nullptr); // 获取当前键盘状态
    if (currentKeyState[SDL_SCANCODE_W]) {
        velocity_.y = -max_speed_;
    }
    if (currentKeyState[SDL_SCANCODE_S]) {
        velocity_.y = max_speed_;
    }
    if (currentKeyState[SDL_SCANCODE_A]) {
        velocity_.x = -max_speed_;
    }
    if (currentKeyState[SDL_SCANCODE_D]) {
        velocity_.x = max_speed_;
    }
}

void Player::syncCamera() {
    game_.getCurrentScene()->setCameraPos(position_ - game_.getScreenSize() / 2.0f); // 设置摄像机位置
}

void Player::checkState() {
    if (velocity_.x < 0) {
        sprite_move_->setFlip(SDL_FLIP_HORIZONTAL); // 设置翻转
        sprite_idle_->setFlip(SDL_FLIP_HORIZONTAL); // 设置翻转
    } else {
        sprite_move_->setFlip(SDL_FLIP_NONE); // 设置翻转
        sprite_idle_->setFlip(SDL_FLIP_NONE); // 设置翻转
    }
    bool new_is_moving = glm::length(velocity_) > 0.1f;
    if (new_is_moving != is_move_) {
        is_move_ = new_is_moving; // 更新移动状态
        changeState(is_move_);
    }
}

void Player::changeState(bool is_moving) {
    if (is_moving) {
        sprite_move_->setActive(is_moving); // 设置精灵动画是否激活
        sprite_idle_->setActive(!is_moving); // 设置精灵动画是否激活
        sprite_move_->setCurrentFrame(sprite_idle_->getCurrentFrame());
        sprite_move_->setFrameTimer(sprite_idle_->getFrameTimer());
    } else {
        sprite_move_->setActive(is_moving); // 设置精灵动画是否激活
        sprite_idle_->setActive(!is_moving); // 设置精灵动画是否激活
        sprite_idle_->setCurrentFrame(sprite_move_->getCurrentFrame());
        sprite_idle_->setFrameTimer(sprite_move_->getFrameTimer());
    }
}

void Player::checkIsDead() {
    if (!stats_->getAlive()) {
        effect_->setActive(true);
        effect_->setPosition(getPosition());
        setActive(false);
        game_.playSound("resources/sound/female-scream-02-89290.mp3");
    }
}
